package com.taptech.doufu.ui.fragment;

import android.app.Fragment;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.dialog.NovelCreateSentTimeDialog;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.ui.view.wheel.adapter.NumericHourWheelAdapter;
import com.taptech.doufu.ui.view.wheel.adapter.NumericMinWheelAdapter;
import com.taptech.doufu.ui.view.wheel.widget.OnWheelScrollListener;
import com.taptech.doufu.ui.view.wheel.widget.WheelView;

/**
 * Created by ${LiYi} on 2016/12/1.
 */

public class SelectTimeFragmet extends Fragment {
    private WheelView wheelViewHour , wheelViewMin;
    private NovelCreateSentTimeDialog.ChangeDate changeDateListener;
    private int curHour,curMin;
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initHour();
        initMin();
        wheelViewHour.setCurrentItem(curHour);
        wheelViewMin.setCurrentItem(curMin);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.select_time_fg,null);
        wheelViewHour = (WheelView)rootView.findViewById(R.id.select_date_fg_year);
        wheelViewMin = (WheelView) rootView.findViewById(R.id.select_date_fg_month);
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            wheelViewHour.setWheelBackground(R.drawable.wheel_night_bg);
            wheelViewHour.setShadowColor(SelectDateFragmet.SHADOWS_COLORS);
            wheelViewMin.setWheelBackground(R.drawable.wheel_night_bg);
            wheelViewMin.setShadowColor(SelectDateFragmet.SHADOWS_COLORS);
        }
        setWheelViewListener();
        return rootView;
    }

    private void setWheelViewListener(){
        wheelViewHour.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                curHour = wheel.getCurrentItem();
                selTimeFinish();
            }
        });
        wheelViewMin.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                curMin = wheel.getCurrentItem();
                selTimeFinish();
            }
        });
    }

    /**
     * 初始时间
     */
    private void initHour() {
        NumericHourWheelAdapter numericWheelAdapter = new NumericHourWheelAdapter(getActivity(),0, 23 , "%02d");
        wheelViewHour.setViewAdapter(numericWheelAdapter);
        wheelViewHour.setCyclic(true);
    }


    private void initMin() {
        NumericMinWheelAdapter numericWheelAdapter = new NumericMinWheelAdapter(getActivity(),0, 11 , "%02d");
        wheelViewMin.setViewAdapter(numericWheelAdapter);
        wheelViewMin.setCyclic(true);
    }

    public void setChangeDate(NovelCreateSentTimeDialog.ChangeDate changeDateListener) {
        this.changeDateListener = changeDateListener;
    }

    public void setCurHour(int curHour) {
        this.curHour = curHour;
    }

    private void selTimeFinish(){
        if(changeDateListener != null){
            changeDateListener.changeTimeListener(curHour,curMin*5);
        }
    }
}
